Option

sealed class Option<out T>

Representation of an optional value. The instance might be either Some value or None.

Parameters

T

Type of the optional value.

Types

Companion
Link copied to clipboard
common
object Companion

Functions

all
Link copied to clipboard
common
inline fun all(predicate: (T) -> Boolean): Boolean
Returns the result of applying the predicate to the value if this is Some or true if this is None.
any
Link copied to clipboard
common
inline fun any(predicate: (T) -> Boolean): Boolean
Returns the result of applying the predicate to the value if this is Some or false if this is None.
asIterable
Link copied to clipboard
common
abstract fun asIterable(): Iterable<T>
Returns an iterable that wraps this Option returning its value if it is defined, or an empty iterable if the option is empty.
asSequence
Link copied to clipboard
common
abstract fun asSequence(): Sequence<T>
Returns a sequence that wraps this Option returning its value if it is defined, or an empty sequence if the option is empty.
filter
Link copied to clipboard
common
inline fun filter(predicate: (T) -> Boolean): Option<T>
Returns the same Some if the predicate is satisfied for the value.
filterIsInstance
Link copied to clipboard
common
inline fun <R> filterIsInstance(): Option<R>
Returns the same Some casted to type R if it is R.
filterNot
Link copied to clipboard
common
inline fun filterNot(predicate: (T) -> Boolean): Option<T>
Returns the same Some if the predicate is not satisfied for the value.
flatMap
Link copied to clipboard
common
inline fun <R> flatMap(transform: (T) -> Option<R>): Option<R>
Maps value of a Some to a new Option using transform or returns the same None.
fold
Link copied to clipboard
common
inline fun <R> fold(default: R, transform: (T) -> R): R
Returns result of applying transform on the value of Some or default if this is None.
inline fun <R> fold(default: () -> R, transform: (T) -> R): R
Returns result of applying transform on the value of Some or default if this is None.
forEach
Link copied to clipboard
common
inline fun forEach(action: (T) -> Unit)
Runs action if this is a Some.
get
Link copied to clipboard
common
abstract fun get(): T
Gets the value of a Some or throws an exception.
getOrNull
Link copied to clipboard
common
abstract fun getOrNull(): T?
Gets the value of a Some or null if this is a None.
iterator
Link copied to clipboard
common
abstract fun iterator(): Iterator<T>
Returns a singleton iterator returning the option's value if it is defined, or an empty iterator if the option is empty.
map
Link copied to clipboard
common
inline fun <R> map(transform: (T) -> R): Option<R>
Maps value of a Some using transform or returns the same None.
none
Link copied to clipboard
common
inline fun none(predicate: (T) -> Boolean): Boolean
Returns false if the predicate is met by the value if this is Some or true otherwise.
toLeft
Link copied to clipboard
common
inline fun <R> toLeft(right: () -> R): Either<T, R>
Returns a Right containing the given argument right if this is empty, or a Left containing this option's value if it is defined.
toList
Link copied to clipboard
common
abstract fun toList(): List<T>
Returns a singleton list containing the option's value if it is defined, or an empty list if the option is empty.
toRight
Link copied to clipboard
common
inline fun <L> toRight(left: () -> L): Either<L, T>
Returns a Left containing the given argument left if this is empty, or a Right containing this option's value if it is defined.
zip
Link copied to clipboard
common
infix fun <R> zip(other: Option<R>): Option<Pair<T, R>>
Returns Some containing a Pair of values of this and other if both Options are Some.
inline fun <T1, R> zip(other: Option<T1>, transform: (T, T1) -> R): Option<R>
Returns Some containing the result of applying transform to both values of this and other if both Options are Some.

Properties

isDefined
Link copied to clipboard
common
val isDefined: Boolean
Returns true if the option is Some.
isEmpty
Link copied to clipboard
common
abstract val isEmpty: Boolean
Returns true if the option is None.
isNotEmpty
Link copied to clipboard
common
val isNotEmpty: Boolean
Returns true if the option is Some.

Inheritors

Some
Link copied to clipboard
None
Link copied to clipboard

Extensions

contains
Link copied to clipboard
common
operator fun <T> Option<T>.contains(element: T): Boolean
Tests whether the Option contains the given element.
evert
Link copied to clipboard
common
fun <T> Option<Try<T>>.evert(): Try<Option<T>>
Moves inner Try outside of the outer Option.
flatten
Link copied to clipboard
common
fun <T> Option<Try<T>>.flatten(): Option<T>
Returns Some if this Some contains a Success.
fun <T> Option<Iterable<T>>.flatten(): List<T>
Returns nested List if this is Some.
fun <T> Option<Option<T>>.flatten(): Option<T>
Transforms a nested Option to a not nested Option.
getOrElse
Link copied to clipboard
common
inline fun <T> Option<T>.getOrElse(default: () -> T): T
Gets the value of a Some or default value if this is None.
orElse
Link copied to clipboard
common
inline fun <T> Option<T>.orElse(default: () -> Option<T>): Option<T>
Returns this Option if this is a Some or default if this is None.
unzip
Link copied to clipboard
common
fun <A, B> Option<Pair<A, B>>.unzip(): Pair<Option<A>, Option<B>>
Transforms an Option of a Pair into a Pair of an Option of the first value and an Option of the second value.
fun <A, B, C> Option<Triple<A, B, C>>.unzip(): Triple<Option<A>, Option<B>, Option<C>>
Transforms an Option of a Triple into a Triple of an Option of the first value, an Option of the second value, and an Option of the third value.